package com.cy.util;

import java.util.HashMap;
import java.util.Map;

public class MutableLongMap<K> {
	private final Map<K, Long> numMap;

	public MutableLongMap(HashMap<K, Long> map) {
		this.numMap = map;
	}

	public static <K> MutableLongMap<K> create() {
		return new MutableLongMap<>(new HashMap<>());
	}

	public long get(K key) {
		return numMap.getOrDefault(key, 0L);
	}

	public long increment(K key) {
		return numMap.compute(key, (key1, num) -> num == null ? 1 : num + 1);
	}

	public long increment(K key, long addNum) {
		return numMap.compute(key, (key1, num) -> num == null ? addNum : num + addNum);
	}

	public Map<K, Long> asMap() {
		if (numMap == null) {
			return new HashMap<>();
		}
		return numMap;
	}


}